module ui {

    export class ProgressBar extends egret.Sprite {

        private track: egret.Bitmap;

        private bar: egret.Bitmap;

        private barWidth: number;

        private timer: egret.Timer;

        private timerCount: number;

        private step: number;

        private count: number;

        public constructor(timerCount: number = 20, step: number = 2) {
            super();
            this.timerCount = timerCount;
            this.count = timerCount;
            this.step = step;

            this.track = manager.ResManager.getBitmap("tyj6443ter_png");
            this.bar = manager.ResManager.getBitmap("rtdfvf_png");
            this.barWidth = this.bar.width;
            this.bar.width = this.bar.width * 0.5;
            this.addChild(this.track);
            this.addChild(this.bar);
            let scale: number = manager.DisplayManager.GAME_WIDTH / 1536;
            this.track.x = (750 - this.track.width) * 0.5 * scale;
            this.track.y = 40;
            this.bar.x = this.track.x + 53;
            this.bar.y = this.track.y + 18;

            this.timer = new egret.Timer(1000);
            this.timer.addEventListener(egret.TimerEvent.TIMER, this.onTimer, this);
            this.timer.start();
        }

        public static readonly TIMER_FINISH: string = "TimerFinish";

        public addCounter(change: number): void {
            this.timerCount += change;
            if (this.timerCount > this.count) {
                this.timerCount = this.count;
            } else if (this.timerCount < 0) {
                this.timerCount = 0;
            }
            this.update();
        }


        public restart(): void {
            this.timer.reset();
            this.timerCount = this.count;
            this.timer.start();
            this.updateView();
            egret.log("start timer");
        }

        public stop(): void {
            this.timerCount = 0;
            this.timer.reset();
            this.bar.width = 0;
            egret.log("stop timer");
        }

        private update(): void {
            if (this.timerCount <= 0) {
                this.timer.reset();
                this.timerCount = 0;
                this.dispatchEvent(new egret.Event(ProgressBar.TIMER_FINISH));
            }
            this.updateView();
        }

        private updateView():void {
            let percent: number = this.timerCount / 10;
            if (percent < 0) {
                percent = 0;
            }
            this.bar.width = this.barWidth * percent;
        }

        private isFlashing: boolean = false;
        private flashCount: number = 0;
        public static readonly FLASH_COUNT: number = 6;

        public playFlash(): void {
            if (this.isFlashing) {
                return;
            }
            this.isFlashing = true;
            this.flashCount = 0;
            this.playFlashImpl();
        }

        private playFlashImpl(): void {
            this.flashCount++;
            this.bar.visible = !this.bar.visible;
            if (this.flashCount > ProgressBar.FLASH_COUNT) {
                this.flashCount = 0;
                this.isFlashing = false;
                this.bar.visible = true;
            } else {
                egret.setTimeout(this.playFlashImpl, this, 100);
            }
        }

        private onTimer(eve: egret.TimerEvent): void {
            this.timerCount -= this.step;
            //egret.log("定时器");
            this.update();
        }
    }

}